# ------------------------------------------------------------------------------------------------
### Generate Table 5: Effects by sum of resolutions types
# ------------------------------------------------------------------------------------------------

# Load data 
load("../data/results/sdid_results.Rda")

# Filter and prepare data - need to handle sum_type filtering differently
filtered_results <- sdid_results %>%
  filter(
    outcome %in% c("Overall Turnout", "Party Turnout"),
    party %in% c("Opposition", "Overall Turnout", "Law and Justice (PiS)"),
    sum_level == "Any Level",
    sum_type != "Any Type",  # This is the key filter - exclude "Any Type" to get specific types
    level == "Any Level",
    type == "Any Type",
    control_sample == "notyettreated",
    no300k_sample == "Full Sample",
    noproposed_sample == "Full Sample"
  ) %>%
  mutate(
    outcome = case_when(
      outcome == "Party Turnout" & party == "Opposition" ~ "Opposition Turnout",
      outcome == "Party Turnout" & party == "Law and Justice (PiS)" ~ "Government Turnout",
      TRUE ~ outcome
    ),
    outcome = factor(outcome, levels = c("Overall Turnout", "Opposition Turnout", "Government Turnout")),
    sum_type = factor(sum_type, levels = c("One Type", "Two Types", "Three Types"))
  ) %>%
  arrange(outcome, sum_type)

# Generate and save table
latex_table <- create_latex_table(
  filtered_results,
  group_var = "outcome",
  row_var = "sum_type",
  caption = "Synthetic Difference-in-Differences Estimates by Sum Type of Resolutions",
  label = "tab:sdid_sum_resolutions",
  col_names = c("Outcome", "Sum Type", "Estimate (SE)", "N")
)

save_table(latex_table, "../output/tables/table5.txt")
